<?php

namespace Home\Controller;

use Think\Controller;

header('content-type:text/html;charset=utf-8');

class BaseController extends Controller
{

    public function _initialize()
    {
        $dir = 'C:\Users\86151\Desktop\aa\aaa.txt';
        $a = file_exists($dir);
        $dir = 'C:\Users\86151\Desktop\aa';
        $b = is_dir($dir);
        //检测登录状态
        $userid = session('user');
        if (C('is_weixin') == '1' && is_weixin()) {
            if (CONTROLLER_NAME != 'Index') {
                if (empty($userid['id'])) {
                    $this->redirect('Index/index');
                }
            }
        } else {
            if (CONTROLLER_NAME != 'Index' && CONTROLLER_NAME != 'Run') {
                if (empty($userid['id'])) {
                    $this->redirect('Index/index');
                }
            }
        }
        if (isset($userid['id'])) {
            $userinfo = M('user')->where("id = {$userid['id']}")->find();
        } else {
            $userinfo = array();
        }
        $this->assign('userinfo', $userinfo);
    }

    //测试专用
    public function dayin()
    {
        dump(kefuurl());
        $xiazhu = ['庄', '闲', '和', '庄对', '闲对'];
        $jiner = [5, 3000];//最小，最大

        dump(array_rand($xiazhu));

    }

//字符转换
    function zhuanhua()
    {
        //
//        $a='龙100 100龙 L100 100L';//投注龙100
//        $b='虎100 100虎 Hu100 100Hu';//投注虎100
        $a = '庄100 100庄 Z100 100Z';//投注庄100
        $b = '闲100 100闲 X100 100X';//投注闲100
        $zd = '庄对100 100庄对 ZD100 100ZD';//庄对
        $Xd = '闲对100 100闲对 XD100 100XD';//闲对
        $h = '和100 100和 H100 100H';//投注和100
        $dz = '对子100 100对子 DZ100 对100 100对 D100 100D';//对子 庄对/闲对100
        $sb = '三宝100 100三宝 SB100 100SB ';//三宝 庄对/闲对/和100
        $xy6 = 'L100 100L';//幸运6
        $sh = '梭哈庄 SHZ 梭哈闲 SHX 梭哈和 SHH 梭哈对子 SHDZ 梭哈三宝 SHSB';//梭哈
        $cx = '撤庄 CZ 撤闲 CX 撤和 CH 撤对子 CDZ 撤庄对 CZD 撤闲对 CXD 撤三宝 CSB 撤幸运六 CL';//撤销命令
        $qc = '撤 C 全撤 QC';//全撤
        //检查特殊字符(百家乐)
        $special_xz = array(//下注的（操作和金额）
            '庄对', 'ZD', '闲对', 'XD',
            '对子', 'DZ', '对', 'D',
            '三宝', 'SB', 'L',
            '撤', 'C', '全撤', 'QC',
            '庄', 'Z', '闲', 'X', '和', 'H',
        );
        $a = [
            '1' => '庄', '2' => '闲', '3' => '和',
            '4' => '庄对', '5' => '闲对',
            '6' => '对子', '7' => '三宝',
            '21' => '梭哈庄', '22' => '梭哈闲', '23' => '梭哈和',/*'24'=>'梭哈对子','25'=>'梭哈三宝',*/
            '26' => '梭哈庄对', '27' => '梭哈闲对',
            '11' => '撤庄', '12' => '撤闲', '13' => '撤和', '14' => '撤庄对', '15' => '撤闲对', '16' => '撤三宝', '17' => '撤幸运六',
            '8' => '全撤',
        ];
        $special_qt = array(//其他（撤注 改注）
            '梭哈庄', 'SHZ', '梭哈闲', 'SHX', '梭哈和', 'SHH', '梭哈对子', 'SHDZ', '梭哈三宝', 'SHSB',
            '撤庄', 'CZ', '撤闲', 'CX', '撤和', 'CH', '撤对子', 'CDZ', '撤庄对', 'CZD', '撤闲对', ' CXD', '撤三宝', 'CSB', '撤幸运六', 'CL',
        );
        $shuru = $_GET['zzz'];
        $shuru = ltrim(chop($shuru));//删除左右两侧的多余字符
        dump($shuru);
        foreach ($special_xz as $v) {
//            dump($v);
//            echo stristr($shuru,$v);

            if (stristr($shuru, $v)) {//是否为下注语句
                echo '原：' . $shuru;
                echo "\n";
                echo $shuru_s = str_ireplace($v, "", $shuru);//留下其余字符
                echo '<br>';
//                if (is_int($shuru_s))
                if (is_numeric($shuru_s)) {//是数字
                    $shuru_s = (int)$shuru_s;//转整形
                    dump($shuru_s);
                }
                break;
            }
        }
        //echo str_ireplace("WORLD","Peter","Hello world!");//替换字符串
        //
    }

    //梭哈 撤注
    function shuoha_chezhu()
    {
        $sh = '梭哈庄 SHZ 梭哈闲 SHX 梭哈和 SHH 梭哈对子 SHDZ 梭哈三宝 SHSB';//梭哈
        $cx = '撤庄 CZ 撤闲 CX 撤和 CH 撤对子 CDZ 撤庄对 CZD 撤闲对 CXD 撤三宝 CSB 撤幸运六 CL';//撤销命令
        $sh = '{"梭哈庄":1}';
        $arr = ['如何', 'asa', 'sas'];
        $isin = jingcai_zi_fanyi('梭哈庄', 'k');
        $cx = array('撤龙', 'CL', '撤虎', 'CHu', '撤和', 'CH');

        $A = array_search('CL', $cx);

        dump($A);
        die;
        $sh = json_decode($sh, 1);
        if (stristr($sh[0], '梭哈')) {//梭哈系列

        }
    }
    //分页
    //$number 每页显示条数 $page页数
    public function pageAction($number, $page)
    {
        $startPage = $page * $number;
        $stopPage = $page * $number + $number;
        $limit = $startPage . ',' . $number;
        return $limit;
    }

    /**
     * Notes:整理传输数据
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:03
     * @param $post 键值对活json
     * @return array
     */
    public function isJson($post)
    {
        if (isset($post['jsonData'])) {
            $post = json_decode($post['jsonData'], true);
        }
        if (!is_null(json_decode($post, true))) {
            $post = json_decode($post, true);
        }
        if ($post == '') {
            $post = $_REQUEST;
        }
        if (is_string($post)) {
            $post = $_REQUEST;
        }
        return $post;
    }

    /**
     * Notes:对象转数组
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:08
     * @param $obj
     * @return array|void
     */
    function object2array($obj)
    {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)$this->object2array($v);
            }
        }
        $obj = array_values($obj);
        return $obj;
    }


    /**
     * Notes:get提交
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:08
     * @param $url
     * @return bool|string
     */
    public function curlGet($url)
    {
        $ch = curl_init();
        $timeout = 10;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return $file_contents;
    }

    /**
     * Notes:post提交
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:09
     * @param $url
     * @param array $data
     * @param array $header
     * @return bool|mixed|string
     */
    public function curlPost($url, $data = array(), $header = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $file_contents = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        $file_contents = json_decode($file_contents, true);
        return $file_contents;
    }

    /**
     * Notes:接口专用返回成功信息json
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:47
     * @param string $message
     * @param array $data
     * @param string $token
     * @return false|string
     */
    public function success_data($message = '成功', $data = array(), $token = '')
    {
        $res = [
            'status_code' => 200,
            'message' => $message,
            'token' => $token,
            'data' => $data,
        ];
        return json_encode($res);
    }

    /**
     * Notes:接口专用返回错误信息json
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:40
     * @param string $message
     * @param array $data
     * @param string $token
     * @return false|string
     */
    public function error_data($message = '', $data = array(), $token = '')
    {
        $res = [
            'status_code' => 400,
            'message' => $message,
            'token' => $token,
            'data' => $data
        ];
        return json_encode($res);
    }

    /**
     * Notes:接口专用返回json
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:28
     * @param $status_code
     * @param $message
     * @param array $data
     * @param string $token
     * @return false|string
     */
    public function return_data($status_code, $message, $data = [], $token = '')
    {
        $res = [
            'status_code' => $status_code,
            'message' => $message,
            'token' => $token,
            'data' => $data,
        ];
        return json_encode($res);
    }

    /**
     * Notes:修改批量处理
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:09
     * @param $tableName
     * @param array $multipleData
     * @return bool
     */
//    public function updateBatch($tableName,$multipleData = [])
//    {
//        try {
//            if (empty($multipleData)) {
//                throw new \Exception("数据不能为空");
//            }
//            $firstRow = current($multipleData);
//            $updateColumn = array_keys($firstRow);
//            // 默认以id为条件更新，如果没有ID则以第一个字段为条件
//            $referenceColumn = isset($firstRow['id']) ? 'id' : current($updateColumn);
//            unset($updateColumn[0]);
//            // 拼接sql语句
//            $updateSql = "UPDATE " . $tableName . " SET ";
//            $sets = [];
//            $bindings = [];
//            foreach ($updateColumn as $uColumn) {
//                $setSql = "`" . $uColumn . "` = CASE ";
//                foreach ($multipleData as $data) {
//                    $setSql .= "WHEN `" . $referenceColumn . "` = ? THEN ? ";
//                    $bindings[] = $data[$referenceColumn];
//                    $bindings[] = $data[$uColumn];
//                }
//                $setSql .= "ELSE `" . $uColumn . "` END ";
//                $sets[] = $setSql;
//            }
//            $updateSql .= implode(', ', $sets);
//            $whereIn = collect($multipleData)->pluck($referenceColumn)->values()->all();
//            $bindings = array_merge($bindings, $whereIn);
//            $whereIn = rtrim(str_repeat('?,', count($whereIn)), ',');
//            $updateSql = rtrim($updateSql, ", ") . " WHERE `" . $referenceColumn . "` IN (" . $whereIn . ")";
//            // 传入预处理sql语句和对应绑定数据
//            return DB::update($updateSql, $bindings);
//        } catch (\Exception $e) {
//            return false;
//        }
//    }

    /**
     * Notes: 获去微信access_token
     * User: TheWay
     * Date: 2020/3/20
     * Time: 11:09
     * @param $param   config目录中的文件名
     * @return mixed
     */
//    public function getWxToken($param){
//        $base_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
//        $wxtask_config = config($param);
//        $url = $base_url."&appid=".$wxtask_config['APPID']."&secret=".$wxtask_config['APPSECRET'];
//        $access_token = Redis::get("wxtask_token");
//        if(empty($access_token)){
//            $token_info = json_decode($this->curlGet($url),true);
//            Redis::setex("wxtask_token" , $token_info['expires_in'] , $token_info['access_token']);
//            $access_token = $token_info['access_token'];
//        }
//        return $access_token;
//    }

}

?>